﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using Common;
using ServiceStack.Redis.Support;
using ServiceStack.Redis;
using System.ServiceModel;
using SpiderContract;
using SpiderServer.Service;
using System.ServiceModel.Activation;
using MySql.Data.MySqlClient;
using System.Net;

namespace SpiderServer
{
	public partial class WinService : ServiceBase
	{
		public WinService()
		{
			InitializeComponent();
		}

		public void Start()
		{
			

			this.OnStart(null);
		}

		protected override void OnStart(string[] args)
		{
			EventLogs.Info("开始启动服务...");
			try
			{
				ServicePointManager.DefaultConnectionLimit = 512;

				ServiceHost host1 = new ServiceHost(typeof(SpiderService));
				host1.Open();

				ServiceHost host2 = new ServiceHost(typeof(AnalyzeService));
				host2.Open();

				CrawlJobInfo[] jobs = Configs.GetStartCrawlJobs();
				if (jobs != null)
				{
					foreach (CrawlJobInfo job in jobs)
					{
						Global.DBAccess.ExecuteNonQuery("update crawl_job set status= 1 where job_id=@job_id", new MySqlParameter("job_id", job.JobId));
						JobManager.Start(job.JobId);
					}
				}
				Threads.Run(new FeedSendThread());
				EventLogs.Info("成功启动服务...");
			}
			catch (Exception ex)
			{
				EventLogs.Error(ex);
			}
		}

		protected override void OnStop()
		{
			Threads.Stop();
		}
	}
}
